/*
 * Created on Sep 27, 2003
 * Local Projects Database
 * Copyright (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Bookmark;
import ro.gateway.aida.obj.db.BookmarkDB;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu
 * BookmarkAction
 * This class handles the bookmarks (list,widget,detail)
 */
public class BookmarkAction extends TilesAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = getServlet().getServletContext();
			PersistenceToken token = PersistenceManager.tokenLookup( sc );
            String mode = request.getParameter("mode");
            User user = UserUtils.getLogged(request, response);

            if (user == null) {
                return mapping.findForward("empty");
            }

            BookmarkDB bmdb = BookmarkDB.getManager(token);
			LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, sc);
			String lang = lang_bean.language;

            if ("delete".equals(mode)) {
                String[] delbm = request.getParameterValues("delbm");
				if (delbm!=null)
                for (int i = 0; i < delbm.length; i++) {
                    bmdb.delete(Long.parseLong(delbm[i]), user.getId());
                }
            }

            if ("insert".equals(mode)) {
                Bookmark b = new Bookmark();
                b.setType(Integer.parseInt(request.getParameter("booktype")));            
                b.setObjectId(Long.parseLong(request.getParameter("objectid")));
				              
                b.setUserId(user.getId());
                bmdb.add(b);

                ActionForward af = new ActionForward();
                af.setPath(b.getUrl());

                return af;
            }

            if ("widget".equals(mode)) {
                ArrayList bookmarks = bmdb.getByUser(user.getId(),lang);
                request.setAttribute("bookmarks", bookmarks);

                return mapping.findForward("widget");
            }

            if ("add".equals(mode)) {
                return mapping.findForward("add");
            }

			
            ArrayList bookmarks = bmdb.getByUser(user.getId(),lang);
			
            PageToken.tokenizeList( request,  sc,  bookmarks, "bookmarks");
			Utils.setBreadCrumbs(request,"bc.bookmarks");	
            return mapping.findForward("list");
        } catch (Exception e) {
			BugTracker.publishBug(e, request);
			return mapping.findForward("fail");
        }
    }
}
